package com.shop.order.controller;

import com.alibaba.fastjson.JSONObject;
import com.base.common.entity.Order;
import com.base.common.entity.PayLog;
import com.base.common.entity.User;
import com.base.common.http.HttpResult;
import com.shop.order.service.OrderService;
import com.shop.order.utils.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * 功能描述：订单增删改查
 */
@RestController
public class OrderController {

    /**
     * 功能描述：注入service对象
     */
    @Autowired
    private OrderService orderService;

    /**
     * 功能描述:添加订单
     */
    @RequestMapping("/add")
    public HttpResult add(String paymentType, HttpServletRequest request, HttpServletResponse response) {

        /** 1、获取token里的用户信息，判断是否已登录 */
        User user = this.getUser(request);

        Order order = new Order();
        //完善订单信息 ，
//        order.setOrderId(Long.valueOf(SnowflakeIdWorker.generateId()));
        order.setStatus("1");
        order.setSourceType("2"); // 2、订单来源PC端
        order.setPaymentType(paymentType); // 1、微信字符 2、货到付款
        order.setCreateTime(new Date());
        order.setUpdateTime(order.getCreateTime());
        order.setUserId(String.valueOf(user.getId()));
        return HttpResult.ok(orderService.addOrder(order, request, response));
    }

    /**
     * 功能描述：根据用户Id获取支付日志
     */
    @RequestMapping("/getPayLogByUserId")
    public HttpResult getPayLogByUserId(Long userId) {
        return HttpResult.ok(this.findPayLogByUserId(userId));
    }

    @RequestMapping("/findPayLogByUserId")
    public PayLog findPayLogByUserId(Long userId) {
        Object payLogByUserId = orderService.getPayLogByUserId(userId);
        return (PayLog) orderService.getPayLogByUserId(userId);
    }

    /**
     * 功能描述：修改订单状态
     * @Param [userId, transactionId, payTime]
     * @Return com.jmyp.http.HttpResult
     * @Author jackhu
     * @Date 2019/7/23
     * @Time 14:28
     */
    @RequestMapping("/updateStatus")
    public HttpResult updateStatus(@RequestParam("userId") String userId, @RequestParam("transactionId") String transactionId, @RequestParam("payTime") String payTime) {
        try {
            orderService.updateStatus(userId, transactionId, payTime);
            return HttpResult.ok("成功!");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return HttpResult.ok("失败!");
    }

    /**
     * 解析请求对象的token得到用户信息
     * @param request
     * @return
     */
    public User getUser(HttpServletRequest request) {

        // 解析请求头中的token
        String userStr = request.getHeader("user");
        User user = null;
        if(userStr != null && userStr != "") {
            user = JSONObject.parseObject(userStr, User.class);
        }
        return user;
    }




}
